//
//  LoginViewController.m
//  insyte
//
//  Created by Vu Dang Tuan on 6/18/15.
//  Copyright (c) 2015 Robert. All rights reserved.
//

#import "LoginViewController.h"
#import "Helpers.h"
#import "UIColor+InsyteColors.h"
#import "LoginEmailViewController.h"
#import "LoginPhoneViewController.h"
#import "TermsViewController.h"
#import "HelpViewController.h"
#import <FBSDKCoreKit/FBSDKCoreKit.h>
#import <FBSDKLoginKit/FBSDKLoginKit.h>
#import "ServiceManager.h"
#import "ProfileStep1ViewController.h"
#import "ProfilesViewController.h"

@interface LoginViewController ()

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initLoginButtomView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (void) initLoginButtomView
{
    [Helpers StyleViewWithBorderColor:_btnLoginWithEmailView color:[UIColor loginButtonColor] corner:5.0f];
    [Helpers StyleViewWithBorderColor:_btnLoginWithFacebookView color:[UIColor loginButtonColor] corner:5.0f];
    [Helpers StyleViewWithBorderColor:_btnLoginWithPhoneView color:[UIColor loginButtonColor] corner:5.0f];
    UITapGestureRecognizer *touchOnEmail = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleLoginWithEmail)];
    UITapGestureRecognizer *touchOnFacebook = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleLoginWithFacebook)];
    UITapGestureRecognizer *touchOnPhone = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleLoginWithPhone)];
    // Set required taps and number of touches
    [touchOnEmail setNumberOfTapsRequired:1];
    [touchOnEmail setNumberOfTouchesRequired:1];
    [touchOnFacebook setNumberOfTapsRequired:1];
    [touchOnFacebook setNumberOfTouchesRequired:1];
    [touchOnPhone setNumberOfTapsRequired:1];
    [touchOnPhone setNumberOfTouchesRequired:1];
    [self.btnLoginWithEmailView addGestureRecognizer:touchOnEmail];
    [self.btnLoginWithFacebookView addGestureRecognizer:touchOnFacebook];
    [self.btnLoginWithPhoneView addGestureRecognizer:touchOnPhone];
    [self.viewMediaController setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.078]];
}

- (void)handleLoginWithEmail
{
   [self.navigationController pushViewController:[[LoginEmailViewController alloc] init] animated:YES];
}

- (void)handleLoginWithFacebook
{
    FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
    [login logInWithReadPermissions:@[@"email"] handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (error) {
            // Process error
        } else if (result.isCancelled) {
            // Handle cancellations
        } else {
            // If you ask for multiple permissions at once, you
            // should check if specific permissions missing
            if ([result.grantedPermissions containsObject:@"email"]) {
                // Do work
                [ServiceManager registerWithFacebook:[FBSDKAccessToken currentAccessToken].tokenString cb:^(BOOL rs) {
                    if (rs) {
                        ProfilesViewController *profile = [[ProfilesViewController alloc] init];
                        UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:profile];
                        [[UIApplication sharedApplication].keyWindow setRootViewController: navController];
                    }
                }];
                NSLog(@"login success");
            }
        }
    }];
}

- (void)handleLoginWithPhone
{
    [self.navigationController pushViewController:[[LoginPhoneViewController alloc] init] animated:YES];
}

- (void)handleLoginWithTerms
{
    [self.navigationController pushViewController:[[LoginPhoneViewController alloc] init] animated:YES];
}
- (IBAction)btnTerms:(id)sender {
    [self.navigationController pushViewController:[[TermsViewController alloc] init] animated:YES];
}

- (IBAction)btnHelp:(id)sender {
    [self.navigationController pushViewController:[[HelpViewController alloc] init] animated:YES];
}


@end
